package com.richapp.launcher.serialport.gateway.command;

import android.util.Log;

import com.richapp.launcher.serialport.gateway.ControlInstructionMessage;
import com.richapp.launcher.serialport.gateway.GatewaytConstant;
import com.richapp.net.util.HexTool;



public abstract class BasicCommand {
	
	// 同步码
	protected String SynchronousCode = "FF";
	// 包长度
	protected String PacketLength = "0012";
	// 包类型
	protected String PacketType = "00";
	// 数据包头
	protected String PacketHead = "FF001200";
	// ID
	protected String ID = "00";
	// 编号
	protected String Number = "0000";
    //路数
    protected String Way = "00";
	// 添加设备
	protected String AddDevice = "0000";
	// 复位
	protected String Reset = "00";
	// 升级
	protected String Upgrade = "00";
	// 开光
	protected String Switch = "00";
	// 调整
	protected String Adjust = "00";
	// 查询
	protected String Query = "00";
	// 读取
	protected String Read = "00";
	// 情景开关
	protected String SceneSwitch_high = "00";
	protected String SceneSwitch_low = "00";

	protected String Dnmber = "00";
	// 校验
	//protected String Check = "0000";

	protected BasicCommand() {
	}
	
	public abstract void analyticalCmd(String cmd);

	public void analyticalInfrared(ControlInstructionMessage message){

	}

	/**
	 * 组合命令
	 * 头      + ID + 路数/编号 +  添加设备 + 复位 + 升级 + 开光 + 调整 + 查询 + 读取 + 情景 + 校验
	 * FF001200  02   0002          0000      00     00      FF     80     00     00    0000   0294
 	 */
	protected String combinationCMD() {
		return PacketHead + ID + Number + Way + AddDevice + Reset + Upgrade + Switch + Adjust + Query + Read + SceneSwitch_high+SceneSwitch_low+ checkCode();
        /*if(GatewaytConstant.getMiltiWayMap().containsKey(ID)){
            return PacketHead + ID + Number + Way + AddDevice + Reset + Upgrade + Switch + Adjust + Query + Read + SceneSwitch_high+SceneSwitch_low+ checkCode();
        }else{
        return PacketHead + ID + Number + AddDevice + Reset + Upgrade + Switch + Adjust + Query + Read + SceneSwitch_high+SceneSwitch_low+ checkCode();
        }*/
	}

	//删除单个探测器设备组合命令
	protected String getResetOnecombinationCMD() {
		return PacketHead + ID + Number + AddDevice + Reset + Upgrade + Switch + Adjust + Query + Read + SceneSwitch_high+SceneSwitch_low+ getResetOneCheckCode();

	}
	
	// 组合命令
	protected String combinationCheckTimeCMD(int time) {
		byte high = (byte)((time & 0xFF00) >> 8);
		byte low = (byte)(time & 0x00FF);
		SceneSwitch_high = HexTool.Byte2Hex(high);
		SceneSwitch_low = HexTool.Byte2Hex(low);

		return PacketHead + ID + Number + Way + AddDevice + Reset + Upgrade + Switch + Adjust + Query + Read + SceneSwitch_high+SceneSwitch_low + checkCode();
        /*if(GatewaytConstant.getMiltiWayMap().containsKey(ID)){
            return PacketHead + ID + Number + Way + AddDevice + Reset + Upgrade + Switch + Adjust + Query + Read + SceneSwitch_high+SceneSwitch_low + checkCode();
        }else{
            return PacketHead + ID + Number + AddDevice + Reset + Upgrade + Switch + Adjust + Query + Read + SceneSwitch_high+SceneSwitch_low + checkCode();
        }*/
	}

	// 生成校验码
	protected String checkCode() {
		String checkCode = null;
		try {
			checkCode = Integer.toHexString(
                              HexTool.HexToInt(SynchronousCode)
                            + HexTool.HexToInt(PacketLength)
                            + HexTool.HexToInt(PacketType)
                            + HexTool.HexToInt(ID)
                            + HexTool.HexToInt(Number)
                            + HexTool.HexToInt(Way)
                            + HexTool.HexToInt(AddDevice)
                            + HexTool.HexToInt(Reset)
                            + HexTool.HexToInt(Upgrade)
                            + HexTool.HexToInt(Switch)
                            + HexTool.HexToInt(Adjust)
                            + HexTool.HexToInt(Query)
                            + HexTool.HexToInt(Read)
                            + HexTool.HexToInt(SceneSwitch_high)
                            + HexTool.HexToInt(SceneSwitch_low));
		} catch (Exception e) {
			e.printStackTrace();
			Log.v("BasicCommand","二进制转换异常");
		}

		if (checkCode.length() == 1) {
			return "000" + checkCode;
		} else if (checkCode.length() == 2) {
			return "00" + checkCode;
		} else if (checkCode.length() == 3) {
			return "0" + checkCode;
		} else if (checkCode.length() == 4) {
			return checkCode;
		}
		return null;
	}


	// 生成校验码适用于删除单个探测器设备组合命令的校验
	protected String getResetOneCheckCode() {
        String checkCode = null;
        checkCode = Integer.toHexString(
                HexTool.HexToInt(SynchronousCode)
                        + HexTool.HexToInt(PacketLength)
                        + HexTool.HexToInt(PacketType)
                        + HexTool.HexToInt(ID)
                        + HexTool.HexToInt(Number)
                        + HexTool.HexToInt(Way)
                        + HexTool.HexToInt(AddDevice)
                        + HexTool.HexToInt(Reset)
                        + HexTool.HexToInt(Upgrade)
                        + HexTool.HexToInt(Switch)
                        + HexTool.HexToInt(Adjust)
                        + HexTool.HexToInt(Query)
                        + HexTool.HexToInt(Read)
                        + HexTool.HexToInt(SceneSwitch_high)
                        + HexTool.HexToInt(SceneSwitch_low)
//                        -HexTool.HexToInt("01")
                );


        if (checkCode.length() == 1) {
            return "000" + checkCode;
        } else if (checkCode.length() == 2) {
            return "00" + checkCode;
        } else if (checkCode.length() == 3) {
            return "0" + checkCode;
        } else if (checkCode.length() == 4) {
            return checkCode;
        }
        return null;

    }
}
